package com.maxd.service.admin;

import com.maxd.error.ExceptionEnum;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.AdminInfo;
import com.maxd.model.Token;
import com.maxd.modules.selfshop.merchants.entity.SelfMerchantApply;
import com.maxd.modules.selfshop.merchants.respository.SelfMerchantApplyJpaRepository;
import com.maxd.modules.selfshop.merchants.respository.SelfStoreJpaRepository;
import com.maxd.respository.admin.AdminJpaRepository;
import com.maxd.respository.admin.AdminRepository;
import com.maxd.respository.sysRole.SysUserRoleRepository;
import com.maxd.respository.token.TokenJpaRepository;
import com.maxd.respository.token.TokenRepository;
import com.maxd.service.sysRole.SysUserRoleService;
import com.maxd.utils.MD5Util;
import com.maxd.utils.UpdateJPAUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 管理平台用户登录的service实现类
 */
@Service
public class IAdminSellingServiceImpl implements IAdminSellingService {
    @Autowired
    private AdminJpaRepository couponJpaRepository;
    @Autowired
    private AdminRepository couponRepository;
    @Autowired
    private TokenJpaRepository tokenJpaRepository;
    @Autowired
    private TokenRepository tokenRepository;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SelfMerchantApplyJpaRepository merchantApplyJpaRepositoryer;
    @Autowired
    private SelfStoreJpaRepository selfStoreJpaRepository;
    @Autowired
    private SysUserRoleRepository sysUserRoleRepository;

    @Override
    public Result findAll() {
        List<AdminInfo> all = couponJpaRepository.findAll();
        return ResultUtil.success(all);
    }

    @Override
    public Page<AdminInfo> selectAdminInfoList(Pageable pageable) {
        return couponRepository.selectAdminInfoList(pageable);
    }

    @Override
    public AdminInfo selectAdminInfoDetails(Long id) {
        return couponRepository.selectAdminInfoDetails(id);
    }

    @Override
    public Result updateAdminInfo(AdminInfo adminInfo) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        AdminInfo adminInfo1 = couponJpaRepository.findById(adminInfo.getId()).orElse(null);
        System.out.println("saveBody-------------------------" + adminInfo.getId());
            assert adminInfo1 != null;
            if (adminInfo.getPwd().equals(adminInfo1.getPwd())) {
                adminInfo.setPwd(adminInfo.getPwd());
            } else {
                adminInfo.setPwd(MD5Util.encoderByMd5(adminInfo.getPwd()));
            }
            adminInfo.setCreateAt(sdf.format(new Date()));
            //修改排除空字段
            UpdateJPAUtil.copyNullProperties(adminInfo1, adminInfo);
            AdminInfo adminInfo2=couponJpaRepository.save(adminInfo);
            sysUserRoleService.saveOrUpdate(adminInfo2.getId(),adminInfo.getRoleIdList());
            return ResultUtil.success();
    }



    @Override
    public Result saveBody(AdminInfo adminInfo) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String phone = adminInfo.getPhone();
        AdminInfo login = couponRepository.login(phone);
        AdminInfo adminInfo1 = couponJpaRepository.findById(adminInfo.getId()).orElse(null);
        System.out.println("saveBody-------------------------" + adminInfo.getId());
        if (adminInfo.getId() == 0) {
            if (login != null) {
                return ResultUtil.error(999, "用户名已注册");
            } else {
                adminInfo.setPwd(MD5Util.encoderByMd5(adminInfo.getPwd()));
                adminInfo.setCreateAt(sdf.format(new Date()));
                AdminInfo adminInfo2=couponJpaRepository.save(adminInfo);
                sysUserRoleService.saveOrUpdate(adminInfo2.getId(),adminInfo.getRoleIdList());
                return ResultUtil.success();
            }
        } else {
            if (login != null) {
                if (login.getId() != adminInfo.getId()) {
                    return ResultUtil.error(999, "用户名已注册");
                }
            }
            assert adminInfo1 != null;
            if (adminInfo.getPwd().equals(adminInfo1.getPwd())) {
                adminInfo.setPwd(adminInfo.getPwd());
            } else {
                adminInfo.setPwd(MD5Util.encoderByMd5(adminInfo.getPwd()));
            }
            adminInfo.setCreateAt(sdf.format(new Date()));
            AdminInfo adminInfo2=couponJpaRepository.save(adminInfo);
            sysUserRoleService.saveOrUpdate(adminInfo2.getId(),adminInfo.getRoleIdList());
            return ResultUtil.success();
        }
    }

    @Override
    public Result findOne(long id) {
        AdminInfo a = couponJpaRepository.findById(id).orElse(null);
        if (a.getMerchantId() != null){
            SelfMerchantApply s = merchantApplyJpaRepositoryer.findById(a.getMerchantId()).orElse(null);
            a.setMerchant(s);
        }
        return ResultUtil.success(a);
    }

    @Override
    public Result Login(String pwd, String name) {
        try {
            if (pwd != null && name != null) {
                AdminInfo login = couponRepository.login(name);

                if (login == null) {
                    return ResultUtil.error(ExceptionEnum.USER_PWD_ERROR);
                }
                if (!MD5Util.checkMD5(pwd, login.getPwd())) {
                    return ResultUtil.error(ExceptionEnum.USER_PWD_ERROR);
                }
                if(login.getAdminType()==2){
                    return ResultUtil.error(-1,"请前往商户端登录");
                }
                long id = login.getId();
                List<Token> byUserId = tokenRepository.findByAdminId(String.valueOf(id));
                if (byUserId != null && byUserId.size()>0) {
                    tokenJpaRepository.deleteAll(byUserId);
                }
                String uuid = UUID.randomUUID().toString();
                Token token = new Token();
                token.setAdminId(String.valueOf(login.getId()));
                token.setUuid(uuid);
                Token save = tokenJpaRepository.save(token);
                return ResultUtil.success(save);
            } else {
                return ResultUtil.error(ExceptionEnum.USER_PWD_EMPTY);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtil.error(ExceptionEnum.USER_NOT_FIND);

    }

    /**
     * 商户端登录
     * @param pwd
     * @param name
     * @return
     */
    @Override
    public Result shopsLogin(String pwd, String name) {
        try {
            if (pwd != null && name != null) {
                AdminInfo login = couponRepository.login(name);

                if (login == null) {
                    return ResultUtil.error(ExceptionEnum.USER_PWD_ERROR);
                }
                if (!MD5Util.checkMD5(pwd, login.getPwd())) {
                    return ResultUtil.error(ExceptionEnum.USER_PWD_ERROR);
                }
                if(login.getAdminType()==1){
                    return ResultUtil.error(-1,"请前往管理端登录");
                }
                long id = login.getId();
                List<Token> byUserId = tokenRepository.findByAdminId(String.valueOf(id));
                if (byUserId != null && byUserId.size()>0) {
                    tokenJpaRepository.deleteAll(byUserId);
                }
                String uuid = UUID.randomUUID().toString();
                Token token = new Token();
                token.setAdminId(String.valueOf(login.getId()));
                token.setUuid(uuid);
                Token save = tokenJpaRepository.save(token);
                return ResultUtil.success(save);
            } else {
                return ResultUtil.error(ExceptionEnum.USER_PWD_EMPTY);
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return ResultUtil.error(ExceptionEnum.USER_NOT_FIND);
    }

    @Override
    public Result delete(long id) {
        couponJpaRepository.deleteById(id);
        return ResultUtil.success();
    }

    @Override
    public Result findByMerchant(String state) {
        return null;
    }

    @Override
    public Result find(String state, Pageable pageable) {
        return null;
    }

    @Override
    @Transactional
    public Result updateBody(AdminInfo foodBanner) {
        BeanUtils.copyProperties(foodBanner, Objects.requireNonNull(couponJpaRepository.findById(foodBanner.getId()).orElse(null)));
        return ResultUtil.success();
    }

    @Override
    public AdminInfo getUserByPhone(String userName) {
        return null;
    }

    @Override
    public Result updateState(String state, String id) {
        return null;
    }

    @Override
    @Transactional
    public Result updatePwd(String phone, String pwd) {
        Integer adminInfo = couponRepository.updatePwd(MD5Util.encoderByMd5(pwd), phone);
        if (adminInfo == 1) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(ExceptionEnum.UPDATE_PWD_ERROR);
        }
    }
}
